from flask import Blueprint, request

from models import Borrow
from service import borrowService

borrow_bp = Blueprint('borrow_bp', __name__)


#查询所有用户的借阅信息
@borrow_bp.route('/', methods=['GET'])
def getAllBorrows():
    return borrowService.getAllBorrowList()


#根据userID查询借阅信息
@borrow_bp.route('/<int:userid>', methods=['GET'])
def getBorrowByUserId(userid):
    return borrowService.getBorrowByuserId(userid)


#添加新的借阅记录
@borrow_bp.route('/<int:bookid>', methods=['POST'])
def addBorrow(bookid):
    data = request.get_json()

    borrow = Borrow(data['user_id'], data['book_id'], data['num'])
    return borrowService.addNewBorrow(borrow, bookid)

#归还图书
@borrow_bp.route('/return/<int:bookid>', methods=['POST'])
def addReturn(bookid):
    data = request.get_json()
    borrow_r = Borrow(data['user_id'], data['book_id'], data['num'])
    borrow_r.id = data['id']
    return borrowService.addNewReturn(borrow_r, bookid)


#删除借阅记录
@borrow_bp.route('/delete', methods=['POST'])
def deleteBorrow():
    data = request.get_json()
    user_id = data['user_id']
    book_id = data['book_id']
    print(user_id, book_id)
    return borrowService.deleteBorrow(user_id, book_id)
